<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.min.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/md5.js"></script>
    <link class="theme" rel="stylesheet" type="text/css" href="./css/color/blue.css" />

</head>

<body>
  <div class="head">
    <div class="head-s">
      <span>修改密码</span>
      <div class="head-l"><i class="fa fa-caret-left" onclick="api.closeWin()"></i></div>
      <!-- <div class="head-r"><i class="fa fa-user"></i></div> -->
    </div>
  </div>
    <!-- <header class="aui-bar aui-bar-nav header">
        <a class="aui-pull-left">
            <span class="aui-iconfont aui-icon-left" onclick="api.closeWin()"></span>
        </a>
        <div class="aui-title">修改密码</div>
        <a class="aui-pull-right aui-btn">
            <span class="fa fa-exclamation" onclick="chongzhi()"></span>
        </a>
    </header> -->
    <div class="header-d"></div>

    <!-- 修改密码 -->
    <div class="aui-content login">
        <ul class="aui-list aui-form-list">
          <li class="aui-list-item">
              <div class="aui-list-item-inner">
                  <div class="aui-list-item-label-icon">
                      <i class="aui-iconfont aui-icon-lock"></i>
                  </div>
                  <div class="aui-list-item-input">
                      <input type="password" placeholder="原密码" name='yuanPassword'>
                  </div>
              </div>
          </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label-icon">
                        <i class="aui-iconfont aui-icon-lock"></i>
                    </div>
                    <div class="aui-list-item-input">
                        <input type="password" placeholder="重置密码" name='password'>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label-icon">
                        <i class="aui-iconfont aui-icon-lock"></i>
                    </div>
                    <div class="aui-list-item-input">
                        <input type="password" placeholder="确认密码" name='confirm'>
                    </div>
                    <!-- <div class="aui-list-item-label-icon">
                    <i class="aui-iconfont aui-icon-display"></i>
                </div> -->
                </div>
            </li>
        </ul>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="modify()">修改</div>
    </div>

</body>
<script type="text/javascript" src="./script/api.js"></script>

</html>
<script>

    apiready = function() {
      var color = $api.getStorage('color');
      $(".theme").attr("href", color);
    }




    function chongzhi() {
        var currentUser = $api.getStorage('currentUser');
        if (currentUser.toLowerCase() != 'superman') {
            api.toast({
                msg: '无法重置所有密码！',
                duration: 2000,
                location: 'bottom'
            });
            return;
        }

        api.confirm({
            title: '',
            msg: '确定要重置所有密码么？',
            buttons: ['确定', '取消']
        }, function(ret, err) {
            var index = ret.buttonIndex;
            if (index==1) {
              var sql = "SELECT * FROM SYsuserList WHERE ugrade !='管理' ";
              var db = api.require('CPDB');
              db.selectSql({
                  name: 'data',
                  sql: sql
              }, function(ret, err) {
                  if (ret.status) {
                      $api.setStorage('userData', ret.data);
                      api.toast({
                          msg: '修改成功！',
                          duration: 2000,
                          location: 'middle'
                      });
                      setTimeout("api.closeWin()", 800);
                  }
              });
            }
        });


    }



    function modify() {
     var yuanPassword = $('input[name="yuanPassword"]').val();
        var password = $('input[name="password"]').val();
        var confirm = $('input[name="confirm"]').val();
        //调取本地storage
        var currentUser = $api.getStorage('currentUser');
        var currentPassWord = $api.getStorage('currentPassWord');

        var userData = $api.getStorage('userData');
        if ($.md5(yuanPassword).toUpperCase()!=currentPassWord) {
          api.toast({
              msg: '原密码不正确！',
              duration: 2000,
              location: 'middle'
          });
          return;
        }

        if (password != '' && password == confirm) {
            for (var i = 0; i < userData.length; i++) {
                if (userData[i].LoginName == currentUser) {
                    userData[i].Password = $.md5(password).toUpperCase();
                    // $api.setStorage('currentPassWord', $.md5(password).toUpperCase());
                }
            }

            $api.setStorage('userData', userData);
            api.toast({
                msg: '修改成功！',
                duration: 2000,
                location: 'middle'
            });
            setTimeout("api.closeWin()", 800);
        } else {
            api.toast({
                msg: '请正确输入密码！',
                duration: 2000,
                location: 'middle'
            });
        }


    }
</script>
